package com.ftf.tp.user.application.service.impl;


import com.ftf.tp.common.constants.SupportTypeEnum;
import com.ftf.tp.user.application.adapter.producer.DomainEventPublisher;
import com.ftf.tp.user.application.dto.SupportDTO;
import com.ftf.tp.user.application.service.UserApplicationService;
import com.ftf.tp.user.domain.action.event.ActionEvent;
import com.ftf.tp.user.application.service.ActionApplicationService;
import com.ftf.tp.user.domain.action.dos.ActionDO;
import com.ftf.tp.user.domain.action.service.ActionService;
import com.ftf.tp.user.domain.action.service.UserService;
import com.ftf.tp.user.domain.data.dos.TpUserDO;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author tanghao
 * @date 2022/8/9
 */
@Slf4j
@Service
public class ActionApplicationServiceImpl implements ActionApplicationService {

    @Resource
    private RocketMQTemplate rocketMqTemplate;

    @Resource
    private DomainEventPublisher domainEventPublisher;

    @Resource
    private ActionService actionService;

    @Resource
    private UserApplicationService userApplicationService;

    /**
     * 用户-行为-点赞
     *
     * @param supportDTO 参数
     */
    @Override
    public void support(SupportDTO supportDTO) {
        String topic = SupportTypeEnum.topicByType(supportDTO.getTargetType());
        ActionDO actionDO = supportDTO.toActionDO();
        TpUserDO tpUserDO = userApplicationService.getUserInfo();
        actionDO.setUserId(tpUserDO.getId());
        actionDO.setNickname(tpUserDO.getNickname());

        //行为入库事件-微服务内
        domainEventPublisher.publishInternal(new ActionEvent(actionDO));

        //文章点赞数据变更 - 发送点赞通知 - 微服务间
        domainEventPublisher.publishExternalSync(new ActionEvent(actionDO), topic);
    }

    /**
     * 行为入库
     *
     * @param actionEvent 点赞dto
     */
    @Override
    public void save(ActionEvent actionEvent) {
        ActionDO actionDO = actionEvent.getData();
        actionService.save(actionDO);
    }
}
